package cn.com.lms.business.organization.service;

import cn.com.lms.business.organization.persistence.entity.FbSysDepartment;
import cn.com.lms.business.organization.persistence.entity.FbSysDepartmentEntity;
import cn.com.lms.business.organization.persistence.repository.FbSysDepartmentRepository;
import com.bnzj.cloud.business.core.persistence.entity.SysDepartment;
import com.bnzj.cloud.business.core.persistence.repository.SysDepartmentRepository;

import com.bnzj.core.persistence.BaseService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author: liuwei
 * @DateTime: 2020/3/11 14:07
 * @Description: 部门业务类
 */
@Service(value = "fbSysDeptService")
public class FbSysDepartmentService extends BaseService<FbSysDepartmentRepository, FbSysDepartmentEntity, Long> {


    /**
     * 获取部门树节点集合
     *
     * @return
     */
    public List<FbSysDepartment> getDeptTreeList() {
        List<FbSysDepartmentEntity> sysDeptList = this.repository.findAll();
        List<FbSysDepartment> parentDeptList = new ArrayList<>();
        List<FbSysDepartment> childDeptList = new ArrayList<>();
        sysDeptList.forEach(item -> {
            FbSysDepartment sysDept = new FbSysDepartment();
            BeanUtils.copyProperties(item, sysDept);
            if (0L == item.getPid()) {
                parentDeptList.add(sysDept);
            } else {
                childDeptList.add(sysDept);
            }
        });
        return getTreeList(parentDeptList, childDeptList);
    }


    /**
     * 获取启用的部门树节点集合
     *
     * @return
     */
    public List<FbSysDepartment> getEnabledDeptTreeList() {
        List<FbSysDepartmentEntity> sysDeptList = this.repository.findAllByEnabled(true);
        List<FbSysDepartment> parentDeptList = new ArrayList<>();
        List<FbSysDepartment> childDeptList = new ArrayList<>();
        sysDeptList.forEach(item -> {
            FbSysDepartment sysDept = new FbSysDepartment();
            BeanUtils.copyProperties(item, sysDept);
            if (0L == item.getPid()) {
                parentDeptList.add(sysDept);
            } else {
                childDeptList.add(sysDept);
            }
        });
        return getTreeList(parentDeptList, childDeptList);
    }

    public List<FbSysDepartment> getTreeList(List<FbSysDepartment> parentList, List<FbSysDepartment> childList) {
        //声明一个map，用来过滤已操作过的数据
        Map<Long, Long> map = Maps.newHashMapWithExpectedSize(childList.size());
        parentList.forEach(item -> getChild(item, map, childList));
        return parentList;
    }

    public void getChild(FbSysDepartment dept, Map<Long, Long> map, List<FbSysDepartment> bodyList) {
        List<FbSysDepartment> childList = Lists.newArrayList();
        bodyList.stream()
                .filter(c -> !map.containsKey(c.getId()))
                .filter(c -> c.getPid().equals(dept.getId()))
                .forEach(c -> {
                    map.put(c.getId(), c.getPid());
                    getChild(c, map, bodyList);
                    childList.add(c);
                });
        dept.setChildList(childList);//子数据集
    }


    // 获取指定部门id下包含的所有子部门id
    public List<Long> specificDeptChildIds(Long specificDeptId, List<FbSysDepartment> deptList) {
        FbSysDepartment specificDept = specificDept(specificDeptId, deptList);
        List<Long> childIds = Lists.newArrayList();
        childIds = getChildIds(specificDept, childIds);
        return childIds;
    }

    private List<Long> getChildIds(FbSysDepartment dept, List<Long> childIds) {
        if (null == dept) {
            return childIds;
        }
        if (null == dept.getChildList() || 0 == dept.getChildList().size()) {
            return childIds;
        }
        for (FbSysDepartment childDept : dept.getChildList()) {
            childIds.add(childDept.getId());
            getChildIds(childDept, childIds);
        }
        return childIds;
    }

    private FbSysDepartment specificDept(Long specificDeptId, List<FbSysDepartment> deptList) {
        for (FbSysDepartment dept : deptList) {
            if (specificDeptId == dept.getId()) {
                return dept;
            }
            if (null == dept.getChildList() || 0 == dept.getChildList().size()) {
                continue;
            }
            for (FbSysDepartment childDept : dept.getChildList()) {
                if (specificDeptId == childDept.getId()) {
                    return childDept;
                }
                if (null == childDept.getChildList() || 0 == childDept.getChildList().size()) {
                    continue;
                }
                FbSysDepartment subCheck = specificDept(specificDeptId, childDept.getChildList());
                if (null != subCheck) {
                    return subCheck;
                }
            }
        }
        return null;
    }

    /*
     * @author liuwei
     * @date 2020/4/21 13:52
     * @desc //给部门全名称字段设置值
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateFullName() {
        this.repository.updateFullName();
    }

}
